//
//  CMCUniqueArray.m
//  reacticons
//
//  Created by Nong Trung Nghia on 8/22/12.
//
//

#import "CMCUniqueArray.h"

@implementation CMCUniqueArray

- (id)init
{
    self = [super init];
    if (self) {
        //
        _array=[[NSMutableArray alloc] init];
    }
    
    return self;
}

-(NSString*)objectAtIndex:(NSUInteger)index{
    if (index >= _array.count) {
        return nil;
    }
    return [_array objectAtIndex:index];
}

-(BOOL)addObject:(NSString*)anObject{
    for (NSString* string in _array) {
        if ([string isEqualToString:anObject]) {
            return NO;
        }
    }
    [_array addObject:anObject];
    return YES;
}

-(NSUInteger)count{
    return _array.count;
}

-(NSString*)stringValue{
    return [_array componentsJoinedByString:@","];
}

-(NSArray*)arrayValue{
    return [_array mutableCopy];
}

-(NSArray*)sortedArray{
    NSMutableArray *_sortArray=[[[NSMutableArray alloc] init] autorelease];

    for (NSString *str in _array) {
        NSMutableDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:
                                  str, @"Sort", nil];
        [_sortArray addObject:dic];
    }
    
    NSSortDescriptor *descriptor =
    [[NSSortDescriptor alloc] initWithKey:@"Sort"
                                ascending:YES
                                 selector:@selector(localizedCaseInsensitiveCompare:)];
    
    NSArray *descriptors = [NSArray arrayWithObjects:descriptor, nil];
    
    _sortArray = [[_sortArray sortedArrayUsingDescriptors:descriptors] mutableCopy];
    
    NSMutableArray *_result=[[NSMutableArray alloc] init];
    for (NSDictionary *dic in _sortArray) {
        [_result addObject:[dic valueForKey:@"Sort"]];
    }
    [_sortArray release];
//    DBLOG(@"%@",_result);
    return [_result autorelease];
}
@end
